'use strict';

var SearchController = function(PublicationService, MessageService, SearchStorageService, $scope) {

	$scope.previousSearchs = SearchStorageService.getAll();
	$scope.searchParams = {};
		
	$scope.types = ['Todos', 'Imagenes', 'Videos', 'Documentos'];
	$scope.searchParams.type = $scope.types[0];
	
	$scope.searchParams.labels=[];
			
	$scope.addNewLabel = function(newLabel) {
		if(newLabel!=undefined){
			$scope.searchParams.labels.push(newLabel);
			$scope.newLabel = '';
		}
	};
	
	$scope.removeLabel = function(index) {
		$scope.searchParams.labels.splice(index, 1);
	};
		
	$scope.resetForm = function() {
		$scope.searchParams = {};
		$scope.searchParams.type = $scope.types[0];
		$scope.searchParams.labels=[];
	};
	
	$scope.search = function(searchParams) {
				
		MessageService.resetError();
		
		var path = $scope.getParamsPath(searchParams);
		var contentType = "";
		
		if(searchParams.type=='Documentos'){
			contentType = "/documents";
		}
		if(searchParams.type=='Videos'){
			contentType = "/videos";
		}
		if(searchParams.type=='Imagenes'){
			contentType = "/images";
		}
		
		PublicationService.searchPublications(contentType, path).then(function(data){
			$scope.results = data;
			SearchStorageService.push(searchParams, $scope.results);
			$scope.previousSearchs = SearchStorageService.getAll();				
        }, function(errorMessage){
        	MessageService.setError(errorMessage);
        });
	};
			
	$scope.getParamsPath = function(searchParams){
		var params = [];
		// All
		$scope.addParamToArray(params, "name", searchParams.name);
		$scope.addParamToArray(params, "description", searchParams.description);
		// Documents
		$scope.addParamToArray(params, "title", searchParams.title);
		$scope.addParamToArray(params, "author", searchParams.author);
		$scope.addParamToArray(params, "pagesgte", searchParams.pagesgte);
		$scope.addParamToArray(params, "pageslte", searchParams.pageslte);
		// Videos
		$scope.addParamToArray(params, "format", searchParams.format);
		$scope.addParamToArray(params, "quality", searchParams.quality);
		$scope.addParamToArray(params, "durationgte", searchParams.durationgte);
		$scope.addParamToArray(params, "durationlte", searchParams.durationlte);
		// Images
		for (var i=0; i<searchParams.labels.length; i++) {
			$scope.addParamToArray(params, "labels", searchParams.labels[i]);
		}
		
		return $scope.buildParamsPath(params);
	};
	
	$scope.addParamToArray = function(params, paramName, param){
		if(param!='' && param!=undefined){
			params.push(paramName + "=" + param);
		}
	};
	
	$scope.buildParamsPath = function(params){
		var path="";
		if(params.length !=0){
			path += "?" + params[0];
			for (var i=1; i<params.length; i++) {
				path += "&" + params[i];
			}
		}
		return path;
	};
	
	$scope.getPreviousSearch = function(selectedSearch){
		$scope.searchParams = $scope.previousSearchs[selectedSearch].params;
		$scope.results = $scope.previousSearchs[selectedSearch].results;
	}
	
	MessageService.resetError();
};